﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Diagnostics;

namespace Master
{
    public class Lane : Label
    {
        public TrafficLight trafficLight = null;

        public Lane()
        {
            this.Type = LaneType.DEFAULT;
            this.Initialized += new EventHandler(Lane_Initialized);
        }
        public static readonly DependencyProperty MaxSpeedProperty = DependencyProperty.Register("MaxSpeed",
typeof(double), typeof(Road), new UIPropertyMetadata((double)10));

        public double MaxSpeed
        {
            get
            {
                return (double)this.GetValue(MaxSpeedProperty);
            }
            set
            {
                this.SetValue(MaxSpeedProperty, value);
            }
        }
        public void setTrafficLight(Grid g)
        {
            TrafficLight t = new TrafficLight();
            t.Height = 50;
            t.Width = 50;
            t.Orientation = Orientation;

            switch (Orientation)
            {
                case 0:
                    t.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    t.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    break;
                case 90:
                    t.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    t.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    break;
                case 180:
                    t.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
                    t.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    break;
                case 270:
                    t.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    t.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    break;
            }

            g.Children.Add(t);
            Grid.SetRow(t, Grid.GetRow(this));
            Grid.SetColumn(t, Grid.GetColumn(this));
            trafficLight = t;
        }

        public Boolean hasTrafficLight()
        {
            if (trafficLight == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        void Lane_Initialized(object sender, EventArgs e)
        {
            switch (Orientation)
            {
                case 0:
                    Background = Brushes.LightSteelBlue;
                    break;
                case 90:
                    Background = Brushes.LightSteelBlue;
                    break;
                case 180:
                    this.Background = Brushes.SpringGreen;
                    break;
                case 270:
                    this.Background = Brushes.SpringGreen;
                    break;
            }
            AllowedDirections = new Direction[1];
            AllowedDirections[0] = Direction.FORWARD;
        }

        public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation",
    typeof(int), typeof(Lane), new UIPropertyMetadata((int)0));

        public int Orientation
        {
            get
            {
                return (int)this.GetValue(OrientationProperty);
            }
            set
            {
                this.SetValue(OrientationProperty, value);
            }
        }

        public static readonly DependencyProperty AllowedDirectionsProperty = DependencyProperty.Register("AllowedDirections",
    typeof(Direction[]), typeof(Lane));

        public Direction[] AllowedDirections
        {
            get
            {
                return (Direction[])this.GetValue(AllowedDirectionsProperty);
            }
            set
            {
                this.SetValue(AllowedDirectionsProperty, value);
            }
        }

        public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type",
typeof(LaneType), typeof(Lane));

        public LaneType Type
        {
            get
            {
                return (LaneType)this.GetValue(TypeProperty);
            }
            set
            {
                this.SetValue(TypeProperty, value);
            }
        }

        public Point getStartPoint()
        {
            Point point = new Point();
            Rect bounds = Toolkit.getBounds(this);
            if (this.Orientation == 0)
            {
                point.X = (bounds.Left + bounds.Right) / 2;
                point.Y = bounds.Bottom;
            }
            else if (this.Orientation == 90)
            {
                point.X = bounds.Left;
                point.Y = (bounds.Top + bounds.Bottom) / 2;
            }
            else if (this.Orientation == 180)
            {
                point.X = (bounds.Left + bounds.Right) / 2;
                point.Y = bounds.Top;
            }
            else if (this.Orientation == 270)
            {
                point.X = bounds.Right;
                point.Y = (bounds.Top + bounds.Bottom) / 2;
            }
            return point;
        }

        public Point getDestinationPoint()
        {
            Point point = new Point();
            Rect bounds = Toolkit.getBounds(this);
            if (this.Orientation == 0)
            {
                point.X = (bounds.Left + bounds.Right) / 2;
                point.Y = bounds.Top;
            }
            else if (this.Orientation == 90)
            {
                point.X = bounds.Right;
                point.Y = (bounds.Top + bounds.Bottom) / 2;
            }
            else if (this.Orientation == 180)
            {
                point.X = (bounds.Left + bounds.Right) / 2;
                point.Y = bounds.Bottom;
            }
            else if (this.Orientation == 270)
            {
                point.X = bounds.Left;
                point.Y = (bounds.Top + bounds.Bottom) / 2;
            }
            return point;
        }

        private InterSection interSection;

        public void setInterSection(InterSection interSection)
        {
            this.interSection = interSection;
        }

        public InterSection getInterSection()
        {
            return interSection;
        }
    }
}
